﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YArchitech.Plumbing
{
	public class ModelAnalyser
	{
		public ModelAnalyser(Autodesk.Revit.DB.Document _doc)
		{
			this.doc = _doc;
		}

		public string GetBuildingVolume()
		{
			string projectInfo;
			if ((projectInfo = this.GetProjectInfo("BuildingVolume", this.doc)) != null)
			{
				return projectInfo;
			}
			return null;
		}

		public string GetBuildingHeight()
		{
			string projectInfo;
			if ((projectInfo = this.GetProjectInfo("BuildingHeight", this.doc)) != null)
			{
				return projectInfo;
			}
			return null;
		}

		public string GetDangerLevel()
		{
			string projectInfo;
			if ((projectInfo = this.GetProjectInfo("DangerLevel", this.doc)) != null)
			{
				return projectInfo;
			}
			return null;
		}

		public string GetBuildingType()
		{
			string projectInfo;
			if ((projectInfo = this.GetProjectInfo("BuildingType", this.doc)) != null)
			{
				return projectInfo;
			}
			return null;
		}

		private string GetProjectInfo(string InfoName, Autodesk.Revit.DB.Document doc)
		{
			string result;
			try
			{
				ProjectInfo projectInformation = doc.ProjectInformation;
				Schema schema = null;
				foreach (Guid guid in projectInformation.GetEntitySchemaGuids())
				{
					Schema schema2 = Schema.Lookup(guid);
					if (schema2.SchemaName == "BuildingInfo")
					{
						schema = schema2;
						break;
					}
				}
				if (schema != null)
				{
					string text = projectInformation.GetEntity(schema).Get<string>(schema.GetField(InfoName));
					if (text != null)
					{
						return text;
					}
				}
				result = null;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = null;
			}
			return result;
		}

		private Document doc;
	}
}
